package model

import "gorm.io/gorm"

type House struct {
	gorm.Model
	UserId     int `gorm:"type:int"`
	Area       int
	RealArea   int
	Price      float64 `gorm:"type:decimal(10,2)"`
	Status     int     `gorm:"type:tinyint;comment:0 待租，1 正在交易，2 已出租，-1已下架"`
	IsRegister int     `gorm:"type:tinyint;comment:1 已认证，0 未认证"`
}

func AddHouse(house House) uint {
	DB.Create(&house)
	return house.ID
}

func DeleteHouse(houseId int) error {
	house := House{}
	house.ID = uint(houseId)
	err := DB.Delete(&house).Error
	if err != nil {
		return err
	}
	return nil
}

func Update(house House) error {
	err := DB.Save(&house).Error
	if err != nil {
		return err
	}
	return nil
}

func SelectHouse(userId int, page int, size int, search string) ([]House, int) {
	var num int64
	DB.Model(&House{}).Count(&num)
	if size == 0 {
		size = 10
	}
	if page <= 0 {
		page = 1
	}
	offset := (page - 1) * size
	if offset > int(num) {
		offset = int(num)
	}
	houseList := []House{}

	DB.Model(&House{}).Limit(size).Offset(offset).Find(&houseList)

	return houseList, page
}
